package com.hybrid.thread;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import com.hybrid.utils.TaoLog;

/**
 * 在此写用途
 *
 * @author: zhiwei
 * @date: 2017-10-09 22:53
 * @version: 9.1.0
 */
public class WVThreadPool {
    private static final int CPU_COUNT = Runtime.getRuntime().availableProcessors();
    private static final int CORE_POOL_SIZE = CPU_COUNT + 1;
    private static final int MAX_POOL_SIZE = CPU_COUNT * 2 + 1;
    private static final int KEEP_ALIVE = 500;

    private static WVThreadPool threadManager;

    private ExecutorService executor = null;

    public synchronized static WVThreadPool getInstance() {
        if (threadManager == null) {
            threadManager = new WVThreadPool();
        }
        return threadManager;
    }

    private WVThreadPool() {
        if (executor == null) {
            executor = new ThreadPoolExecutor(CORE_POOL_SIZE, MAX_POOL_SIZE, KEEP_ALIVE, TimeUnit.MILLISECONDS,
                new LinkedBlockingQueue<Runnable>());
        }
    }

    public void execute(Runnable command) {
        if (command == null) {
            TaoLog.w("WVThreadPool", "execute task is null.");
            return;
        }
        executor.execute(command);
    }
}
